package org.quickjsf.jsf.listener;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.apache.commons.lang3.StringUtils;
import org.quickjsf.common.util.FacesUtil;

//TODO 改为全局
public class FormValidationPhaseListener implements PhaseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2987940903069096122L;

	@Override
	public void afterPhase(PhaseEvent event) {
		System.out.println("After:" + event.getPhaseId().toString());
		if (event.getFacesContext().isValidationFailed()) {
//			FacesUtil.getUIComponent(clientId);
			String formId = FacesUtil.getRequestParameter("submit_form_id");
			if (!StringUtils.isEmpty(formId)) {
//				event.getFacesContext().getPartialViewContext().getRenderIds().clear();
////				event.getFacesContext().getPartialViewContext().getRenderIds().add("growl"); 
//				event.getFacesContext().getPartialViewContext().getRenderIds().add(formId); 
			}
		}
	}

	@Override
	public void beforePhase(PhaseEvent event) {
		System.out.println("Before:" + event.getPhaseId().toString());
	}

	@Override
	public PhaseId getPhaseId() {
		
		return PhaseId.PROCESS_VALIDATIONS;
	}

}
